package com.cx.util;

import android.graphics.Paint;
import android.text.TextPaint;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;

public class ViewHelper {

    public static void bindToTextView(@NonNull TextView view, String content) {
        if (TextUtils.isEmpty(content))
            content = "";
        view.setText(content);
    }

    public static void bindToTextView(@NonNull TextView view, int num) {
        view.setText(String.valueOf(num));
    }


    public static void setVisible(View view, boolean visible) {
        Preconditions.checkNotNull(view);
        if (visible) {
            view.setVisibility(View.VISIBLE);
        } else {
            view.setVisibility(View.GONE);
        }
    }

    public static void addTextLine(@NonNull TextView view) {
        TextPaint paint = view.getPaint();
        paint.setFlags(Paint.UNDERLINE_TEXT_FLAG);
        paint.setAntiAlias(true);
    }

    public static void bindToTextView(@NonNull EditText editText, String content) {
        bindToTextView((TextView) editText, content);
        if (editText.hasFocus())
            moveToEnd(editText);
    }


    public static void moveToEnd(@NonNull EditText editText) {
        String string = editText.getText().toString();
        int index = string.length() - 1;
        editText.setSelection(index);
    }

    public static void bindToTextView(@NonNull EditText editText, int content) {
        bindToTextView(editText, String.valueOf(content));
    }

}
